package cn.minimelon.cushaw.impl;

import cn.minimelon.bittermelon.bean.LoginUser;
import cn.minimelon.cushaw.dao.SysMenuDao;
import cn.minimelon.cushaw.entity.SysDatadef;
import cn.minimelon.cushaw.entity.SysMenu;
import cn.minimelon.cushaw.service.SysDatadefService;
import cn.minimelon.cushaw.service.SysMenuService;
import org.beetl.sql.ext.solon.Db;
import org.noear.solon.annotation.Inject;
import org.noear.solon.extend.aspect.annotation.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SysMenuServiceImpl implements SysMenuService {
    @Db
    SysMenuDao menuDao;

    @Inject
    SysDatadefService datadefService;

    @Override
    public List<SysMenu> selectAll(LoginUser user) {
        List<SysMenu> menuList = menuDao.all();
        return this.buildMenuTree(menuList);
    }

    /**
     * 构建前端所需要树结构
     *
     * @param menus 菜单列表
     * @return 树结构列表
     */
    @Override
    public List<SysMenu> buildMenuTree(List<SysMenu> menus) {
        List<SysMenu> returnList = new ArrayList<>();
        List<String> tempList = new ArrayList<>();
        for (SysMenu dept : menus) {
            tempList.add(dept.getMenuId());
        }
        for (SysMenu menu : menus) {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(menu.getParentId())) {
                recursionFn(menus, menu);
                returnList.add(menu);
            }
        }
        if (returnList.isEmpty()) {
            returnList = menus;
        }
        return returnList;
    }

    @Override
    public List<SysMenu> selectAreaMenus(String area) {
        List<SysDatadef> defList = datadefService.selectByArea(area);
        return defList.stream().map(this::generateMenu).collect(Collectors.toList());
    }

    /**
     * 生成模型菜单
     */
    private SysMenu generateMenu(SysDatadef def) {
        SysMenu menu = new SysMenu();
        menu.setMenuId(def.getCode().toUpperCase());
        menu.setParentId("0");
        menu.setMenuName(def.getName());
        menu.setIcon("BorderOuterOutlined");
        menu.setMenuType("M");
        {
            Map<String, String> links = new HashMap<>();
            {
                links.put("query", "查询");
                links.put("update", "申请");
                links.put("audit", "审核");
            }
            for (Map.Entry<String, String> entry : links.entrySet()) {
                SysMenu c = new SysMenu();
                c.setMenuId(def.getCode().toUpperCase() + entry.getKey().toUpperCase());
                c.setParentId(def.getCode());
                c.setMenuName(def.getName() + entry.getValue());
                c.setMenuType("C");
                c.setPath("/view/" + def.getAcode().toLowerCase() + "/" + def.getCode().toLowerCase() + "/" + entry.getKey().toLowerCase());
                menu.getChildren().add(c);
            }
        }
        return menu;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<SysMenu> list, SysMenu t) {
        // 得到子节点列表
        List<SysMenu> childList = getChildList(list, t);
        t.setChildren(childList);
        for (SysMenu tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<SysMenu> getChildList(List<SysMenu> list, SysMenu t) {
        return list.stream()
                .filter(n -> n.getParentId().equals(t.getMenuId()))
                .collect(Collectors.toList());
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<SysMenu> list, SysMenu t) {
        return getChildList(list, t).size() > 0;
    }
}
